package com.example.viewpager2

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.example.viewpager2.databinding.ActivityMainBinding
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import me.relex.circleindicator.CircleIndicator3

class MainActivity : AppCompatActivity() {


    private val fragments = mutableListOf<Fragment>()

    private val mTitles  = mutableListOf<String>("首页","个人中心")
    private val mTabEntities  = arrayListOf<CustomTabEntity>()
    private val mIconSelectIds  = arrayListOf<Int>(R.drawable.home_selector,R.drawable.k_selector)
    private val mIconUnselectIds  = arrayListOf<Int>(R.drawable.home_normal,R.drawable.k_normal)

    private val mData = mutableListOf<Data>()

    private val binding : ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        for (i in 0 until mTitles.size) {
            mTabEntities.add(
                TabEntity(
                    mTitles[i],
                    mIconSelectIds[i],
                    mIconUnselectIds[i]
                )
            )
        }
        fragments.add(HomeFragment())
        fragments.add(MineFragment())
        binding.tablayout.setTabData(mTabEntities)

        val superList = mutableListOf<MutableList<SubData>>()
        val subList1 = mutableListOf<SubData>()
        subList1.add(SubData("https://img1.baidu.com/it/u=995912340,2234084405&fm=26&fmt=auto","老虎"))
        subList1.add(SubData("https://img2.baidu.com/it/u=1357720467,2636512316&fm=26&fmt=auto","公路"))
        subList1.add(SubData("https://img2.baidu.com/it/u=1640548213,2648418637&fm=26&fmt=auto","雪山"))
        subList1.add( SubData("https://img2.baidu.com/it/u=4286820487,1240630332&fm=26&fmt=auto","大山"))
        subList1.add(SubData("https://img0.baidu.com/it/u=1051577226,2771334401&fm=26&fmt=auto","夕阳"))

        superList.add(subList1)
        val subList2 = mutableListOf<SubData>()
        subList2.add(SubData("https://img1.baidu.com/it/u=995912340,2234084405&fm=26&fmt=auto","老虎"))
        subList2.add(SubData("https://img0.baidu.com/it/u=1578762854,2915076460&fm=26&fmt=auto","公路"))
        subList2.add(SubData("https://img0.baidu.com/it/u=1061904017,1312817620&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500","雪山"))
        subList2.add( SubData("https://img2.baidu.com/it/u=4286820487,1240630332&fm=26&fmt=auto","大山"))
        subList2.add(SubData("https://img0.baidu.com/it/u=1051577226,2771334401&fm=26&fmt=auto","夕阳"))
        superList.add(subList2)
        val subList3 = mutableListOf<SubData>()
        subList3.add(SubData("https://img1.baidu.com/it/u=995912340,2234084405&fm=26&fmt=auto","老虎"))
        subList3.add(SubData("https://img2.baidu.com/it/u=1357720467,2636512316&fm=26&fmt=auto","公路"))
        subList3.add(SubData("https://img2.baidu.com/it/u=1640548213,2648418637&fm=26&fmt=auto","雪山"))
        subList3.add( SubData("https://img0.baidu.com/it/u=3458844506,1034627623&fm=26&fmt=auto","大山"))
        subList3.add(SubData("https://img0.baidu.com/it/u=1051577226,2771334401&fm=26&fmt=auto","夕阳"))
        superList.add(subList3)
        val subList4 = mutableListOf<SubData>()
        subList4.add(SubData("https://img1.baidu.com/it/u=995912340,2234084405&fm=26&fmt=auto","老虎"))
        subList4.add(SubData("https://img2.baidu.com/it/u=1357720467,2636512316&fm=26&fmt=auto","公路"))
        subList4.add(SubData("https://img2.baidu.com/it/u=1683995662,3686817099&fm=26&fmt=auto","雪山"))
        subList4.add( SubData("https://img1.baidu.com/it/u=3427821080,1838386749&fm=26&fmt=auto","大山"))
        subList4.add(SubData("https://img0.baidu.com/it/u=2014147973,3096366009&fm=26&fmt=auto","夕阳"))
        superList.add(subList4)
        val adapter = MyPagerAdapter()
        mData.add(Data(0,superList))
        mData.add(Data(1,title = "title2"))
        adapter.setData(mData)
        binding.tablayout.setOnTabSelectListener(object :OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                binding.viewPager2.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
       binding.viewPager2.registerOnPageChangeCallback(object :ViewPager2.OnPageChangeCallback(){
           override fun onPageSelected(position: Int) {
               super.onPageSelected(position)
               Log.e("日志", "onPageSelected: ${position}")
               binding.tablayout.currentTab = position
           }
       })
        binding.viewPager2.adapter = adapter
    }

    class MyPagerAdapter :RecyclerView.Adapter<RecyclerView.ViewHolder>(){

        companion object{
            const val TYPE1 = 0
            const val TYPE2 = 1
        }

        private var mData = mutableListOf<Data>()
        fun setData(data:MutableList<Data>){
            this.mData = data
        }
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            var view  :View
            if (viewType == TYPE2){
                view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout2,parent,false)
                return  MyHolder(view)
            } else{
                view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout,parent,false)
                return  RecycleViewHolder(view)
            }

        }

        class MyHolder(itemView: View) :RecyclerView.ViewHolder(itemView){
            val textView = itemView.findViewById<TextView>(R.id.tv)
        }

        class RecycleViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView){
            val viewPager = itemView.findViewById<ViewPager2>(R.id.subViewPager)
            val indicator = itemView.findViewById<CircleIndicator3>(R.id.indicator)

        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val viewType = getItemViewType(position)
            if (viewType == TYPE2){
                val myHolder = holder as MyHolder
                myHolder.textView.text = "postion :${position}  textView"
            } else{
                val holder2 = holder as RecycleViewHolder
                holder2.viewPager.requestDisallowInterceptTouchEvent(true)
                val subViewPagerAdapter = SubViewPagerAdapter()
                val subData = mData[position].subDatas!!
                subViewPagerAdapter.setData(subData)
                holder2.viewPager.adapter = subViewPagerAdapter
                holder2.indicator.setViewPager(holder2.viewPager)
            }
        }

        override fun getItemViewType(position: Int): Int {
            return if (mData[position].type == 1) TYPE2 else TYPE1
        }

        override fun getItemCount(): Int {
            return mData.size
        }
    }


    private class SubViewPagerAdapter :RecyclerView.Adapter<SubViewPagerAdapter.SubViewPagerHolder>(){

        private var mData = mutableListOf<MutableList<SubData>>()
        fun setData(data:MutableList<MutableList<SubData>>){
            this.mData = data
        }
        class SubViewPagerHolder(itemView:View) : RecyclerView.ViewHolder(itemView){
            val recycleView = itemView.findViewById<RecyclerView>(R.id.rv)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubViewPagerHolder {
            return SubViewPagerHolder(LayoutInflater.from(parent.context).inflate(R.layout.sub_viewpager_item_layout,parent,false))
        }

        override fun onBindViewHolder(holder: SubViewPagerHolder, position: Int) {
            val linearLayoutManager = object :LinearLayoutManager(holder.recycleView.context,LinearLayoutManager.HORIZONTAL,false){
                override fun canScrollHorizontally(): Boolean {
                    return false
                }
            }
            holder.recycleView.layoutManager = linearLayoutManager
            val adapter = MyRecyclerViewAdapter()
            adapter.setData(mData[position])
            holder.recycleView.adapter =adapter
        }

        override fun getItemCount(): Int {
            return mData.size
        }
    }


    class MyRecyclerViewAdapter :RecyclerView.Adapter<MyRecyclerViewAdapter.Holder1>(){


        private var mData = mutableListOf<SubData>()

        fun setData(data:MutableList<SubData>){
            this.mData = data
        }

        inner class Holder1(itemView:View) :RecyclerView.ViewHolder(itemView){
            val textView = itemView.findViewById<TextView>(R.id.texView)
            val circleProgressbarView = itemView.findViewById<DownloadProgressbarView>(R.id.solid_progress)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder1 {
            return Holder1(LayoutInflater.from(parent.context).inflate(R.layout.sub_item_layout,parent,false))
        }

        override fun onBindViewHolder(holder: Holder1, position: Int) {
            val progressbarView = holder.circleProgressbarView
            holder.textView.text = mData[position].title
            progressbarView.setImageUrl(mData[position].url)
            progressbarView.setOnClickListener {
                Log.e("日志", "onBindViewHolder: ")
//                holder.circleProgressbarView.visibility = View.VISIBLE
//                val animator = ValueAnimator.ofInt(0,100)
//                animator.addUpdateListener(object :ValueAnimator.AnimatorUpdateListener{
//                    override fun onAnimationUpdate(animation: ValueAnimator?) {
//                        animation?.let {
//                            holder.circleProgressbarView.setProgress(it.animatedValue as Int)
//                        }
//                    }
//                })
//
//                animator.setRepeatCount(ValueAnimator.INFINITE);
//                animator.setDuration(4000);
//                animator.start();
            }
        }

        override fun getItemCount(): Int {
            return mData.size
        }

    }

}


